package com.ihd.ihardware.common.widget;


import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.ihd.ihardware.R;

public class FullProgressView extends View {
    private static final String TAG = "SportProgressView";
    private PaintFlagsDrawFilter mPaintFlagsDrawFilter;
    private Paint mPaint;
    private int[] mColors = {Color.parseColor("#6F6AF0"), Color.parseColor("#936AE5"),};
    private int colorEmpty = Color.parseColor("#181858");

    private int mWidth; // 控件的宽高
    private int mHeight;

    private int mProgressWidth;
    private int mCurProgress;

    private Context mContext;

    public FullProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FullProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttr(attrs, defStyleAttr);

        init();
    }

    private void initAttr(AttributeSet attrs, int defStyleAttr) {
        TypedArray a = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.SportProgressView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.SportProgressView_startColor:
                    mColors[0] = a.getColor(attr, mColors[0]);
                    break;
                case R.styleable.SportProgressView_endColor:
                    mColors[1] = a.getColor(attr, mColors[1]);
                    break;
                case R.styleable.SportProgressView_emptyColor:
                    colorEmpty = a.getColor(attr, colorEmpty);
                    break;
                case R.styleable.SportProgressView_progressWidth:
                    mProgressWidth = a.getDimensionPixelSize(attr, 14);
                    break;
                default:
                    break;
            }
        }
        a.recycle();
    }

    /**
     * 初始化
     */
    private void init() {
        mPaintFlagsDrawFilter = new PaintFlagsDrawFilter
                (0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(mProgressWidth);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.EXACTLY) {
            mWidth = widthSpecSize;
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            mWidth = 500;
        }

        if (heightSpecMode == MeasureSpec.EXACTLY) {
            mHeight = heightSpecSize;
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            mHeight = 500;
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    private float mProgress;

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.setDrawFilter(mPaintFlagsDrawFilter);
        RectF rect = new RectF(mProgressWidth / 2 + getPaddingLeft(), mProgressWidth / 2 + getPaddingTop(), mWidth - mProgressWidth / 2, mHeight - mProgressWidth / 2);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(colorEmpty);
        mPaint.setShader(null);
        canvas.drawArc(rect, 0, 360, false, mPaint);

        /**
         * 设置渐变颜色
         */
        LinearGradient linearGradient = new LinearGradient(getMeasuredWidth(), getMeasuredHeight(), mWidth, 0, mColors,
                null, LinearGradient.TileMode.CLAMP);
        mPaint.setShader(linearGradient);
        canvas.drawArc(rect, 270, mProgress, false, mPaint);
    }

    public void setProgress(int progress) {
        mCurProgress = progress;
        startAnimation();
    }

    /**
     * 动画效果的取值
     */
    public void startAnimation() {
        mProgress = 0f;
        ValueAnimator animator = ValueAnimator.ofFloat(0, mCurProgress / 100f * 360);
        animator.setDuration(2500).start();
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mProgress = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
    }

    private int dp2px(float dp) {
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        return (int) (dp * metrics.density);
    }
}